<template>
  <div id="cluereport">
    <el-dialog
      :title="dialogtitle"
      :visible.sync="porjectEidtFormVisible"
      width="98%" top="20px" :close-on-click-modal="false" :close-on-press-escape="false">
      <projectEdit :id="projectId" :taskId="$route.params.id" :readOnly="true" :timestamp="timestamp"></projectEdit>
    </el-dialog>
    <el-dialog
      title="修改名称"
      :visible.sync="editTitleFormVisible"
      width="800px" top="20px" :close-on-click-modal="false" :close-on-press-escape="false">
      <el-form inline ref="editForm" style="padding:10px 0 0;clear:both">
        <el-form-item prop="title">
          <el-input
            v-model="itemTitle"
            style="width:600px;"
            :maxlength="100"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click.native="saveTitle" :loading="submitLoading">保存</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
    <el-dialog
      title="退回"
      :visible.sync="formShowVisible"
      width="98%" top="20px" :close-on-click-modal="false" :close-on-press-escape="false">
      <el-form label-width="200px" ref="editForm" style="padding:10px 0 0;clear:both">
        <el-form-item label="名称">
          {{itemName}}
        </el-form-item>
        <el-form-item prop="backReason" label="退回原因">
          <el-input
            v-model="backReason"
            style="width:400px;"
            :maxlength="200"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click.native="backSubmit()" :loading="submitLoading">退回</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
    <el-breadcrumb separator=">">
      <el-breadcrumb-item :to="{ path: '/' }">系统首页</el-breadcrumb-item>
      <el-breadcrumb-item :to="{ path: '/meeting' }">司内会议</el-breadcrumb-item>
      <el-breadcrumb-item >管理</el-breadcrumb-item>
    </el-breadcrumb>
    <section class="wboxs">
    <!--工具条-->
      <h1>司内会议名称：{{project}}</h1>
      <el-button-group style="margin:10px" v-if="viewtype===1">
        <el-button type="primary" icon="el-icon-arrow-up" :loading="btnLoading" @click="moveData(-1)" :disabled="!moveUpFlag">上移</el-button>
        <el-button type="primary" icon="el-icon-arrow-down" :loading="btnLoading" @click="moveData(1)" :disabled="!moveDownFlag">下移</el-button>
      </el-button-group>
    </section>
    <section class="unitbox" style="margin-top:10px">
    <!-- <section style="margin-top:10px"> -->
      <!--列表-->
      <el-table :data="list" highlight-current-row v-loading="listLoading" style="width: 100%;" @sort-change='sortChange' :height="tableHeight" border
        @selection-change="handleSelectionChange"
        ref="multipleTable"
        >
        <el-table-column type="selection" width="40" />
        <!-- <el-table-column type="index" label="序号" width="60" align="center" class-name="tableth">
        </el-table-column> -->
        <el-table-column label="名称" prop="title">
          <template slot-scope="scope">
            {{scope.row.title}} <el-button type="text" icon="el-icon-edit" style="color:blue !important" @click="editTitle(scope.row.id,scope.row.title)" />
          </template>
        </el-table-column>
        <!-- <el-table-column label="附件个数" prop="filesNum" width="100" >
          <template slot-scope="scope">
            {{scope.row.state===1?scope.row.filesNum:''}}
          </template>
        </el-table-column> -->
        <el-table-column label="处室" prop="deptName" width="150" align="center">
        </el-table-column>
        <el-table-column label="姓名" prop="userRealName" width="100" align="center">
        </el-table-column>
        <el-table-column label="提交时间" prop="submitTime" width="200" align="center">
        </el-table-column>
        <el-table-column label="操作" width="300" align="center">
          <template slot-scope="scope">
            <el-button @click="detail(scope.row.id)" type="primary" size="mini" style="margin:10px 0 10px">查看</el-button>
            <el-button type="warning" @click="backData(scope.row.title,scope.row.id)" size="mini" v-if="meetingAdmin">退回</el-button>
            <el-button type="danger" @click="removeData(scope.row.id)" size="mini" v-if="meetingAdmin">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </section>
  </div>
</template>

<script>
import projectEdit from './detail'
export default {
  components: {
    projectEdit
  },
  data () {
    return {
      editTitleFormVisible: false,
      itemId: null,
      itemTitle: '',
      filters: {
        key: '',
        status: null
      },
      moveDownFlag: false,
      moveUpFlag: false,
      btnLoading: false,
      projectId: null,
      dialogtitle: '新建',
      timestamp: '',
      porjectEidtFormVisible: false,
      porjectEidtFormLoading: false,
      meetingAdmin: true,
      viewtype: 1,
      project: '',
      listLoading: true,
      tableHeight: 100,
      list: [
      ],
      changeSizeFlag: false,
      itemName: '',
      projectDataId: '',
      backReason: '',
      formShowVisible: false,
      submitLoading: false,
      multipleSelection: []
    }
  },
  methods: {
    toggleSelection (id) {
      this.$nextTick(() => {
        if (id) {
          let newrow = this.list.find(item => item.id === id)
          if (newrow) {
            this.$refs.multipleTable.toggleRowSelection(newrow)
          }
        } else {
          this.$refs.multipleTable.clearSelection()
        }
      })
    },
    moveData (step) {
      this.btnLoading = true
      let para = {
        id: this.multipleSelection[0].id,
        step: step
      }
      this.$http.post(this.$http.defaults.meetingAPI + `api/meetingBusItem/switchOrder`, para).then(response => {
        console.log(response.data.success)
        if (response.data.success === true) {
          this.btnLoading = false
          this.getList(this.multipleSelection[0].id)
        } else {
          console.log('调整顺序失败:' + response.data.message)
          this.$message({
            message: '调整顺序失败:' + response.data.message,
            type: 'error'
          })
          this.btnLoading = false
        }
      }).catch(error => {
        console.log(error)
        this.$message({
          message: '调整顺序失败:' + error,
          type: 'error'
        })
        this.btnLoading = false
      })
    },
    selectTeams () {
      this.exportVisible = true
    },
    detail (id) {
      this.dialogtitle = '查看'
      this.projectId = id
      this.timestamp = new Date().getTime()
      this.porjectEidtFormVisible = true
    },
    editTitle (id, title) {
      this.itemId = id
      this.itemTitle = title
      this.editTitleFormVisible = true
    },
    saveTitle () {
      if (!this.itemTitle || this.itemTitle === '') {
        this.$message({
          message: '请填写名称',
          type: 'error'
        })
        return false
      }
      this.submitLoading = true
      let para = {
        title: this.itemTitle,
        id: this.itemId
      }
      this.$http
        .post(this.$http.defaults.meetingAPI + `api/meetingBusItem/updateTitle`, para)
        .then(response => {
          if (response.data.success) {
            this.$message({
              message: '保存成功！',
              type: 'success'
            })
            this.editTitleFormVisible = false
            this.getList()
          } else {
            this.$message({
              message: '保存失败:' + response.data.message,
              type: 'error'
            })
          }
          this.submitLoading = false
        })
        .catch(error => {
          this.submitLoading = false
          this.$message({
            message: '网络提交失败:' + error,
            type: 'error'
          })
        })
    },
    backSubmit () {
      if (!this.backReason || this.backReason === '') {
        this.$message({
          message: '请填写退回原因',
          type: 'error'
        })
        return false
      }
      this.submitLoading = true
      let para = {
        reason: this.backReason,
        id: this.projectDataId
      }
      this.$http
        .post(this.$http.defaults.meetingAPI + `api/meetingBusItem/backItem`, para)
        .then(response => {
          if (response.data.success) {
            this.$message({
              message: '退回操作成功！',
              type: 'success'
            })
            this.formShowVisible = false
            this.getList()
          } else {
            this.$message({
              message: '退回失败:' + response.data.message,
              type: 'error'
            })
          }
          this.submitLoading = false
        })
        .catch(error => {
          this.submitLoading = false
          this.$message({
            message: '网络提交失败:' + error,
            type: 'error'
          })
        })
    },
    backData (name, projectDataId) {
      this.itemName = name
      this.projectDataId = projectDataId
      this.formShowVisible = true
    },
    removeData (id) {
      this.$confirm('确认删除该信息吗?', '提示', {
        type: 'error'
      }).then(() => {
        this.$http.post(this.$http.defaults.meetingAPI + `api/meetingBusItem/remove`, {ids: [id]}).then(response => {
          if (response.data.success === true) {
            this.$message({
              message: '删除成功',
              type: 'success'
            })
          } else {
            console.log('删除失败:' + response.data.message)
            this.$message({
              message: '删除失败:' + response.data.message,
              type: 'error'
            })
          }
          this.getList()
        })
      }).catch(() => {
      })
    },
    stateText (state) {
      switch (state) {
        case 0:
          return '未上报'
        case 1:
          return '<span style="color:green;font-weight:bolder"><i class="el-icon-success" /> 已上报</span>'
        case 2:
          return '<span style="color:red;font-weight:bolder"><i class="el-icon-error" /> 已退回</span>'
      }
    },
    handleSelectionChange (val) {
      this.moveUpFlag = false
      this.moveDownFlag = false
      this.multipleSelection = val
      if (this.multipleSelection.length === 1) {
        this.moveUpFlag = true
        this.moveDownFlag = true
        if (this.list.findIndex(item => item.id === this.multipleSelection[0].id) === 0) {
          this.moveUpFlag = false
        }
        if (this.list.findIndex(item => item.id === this.multipleSelection[0].id) === this.list.length - 1) {
          this.moveDownFlag = false
        }
      }
    },
    handleSizeChange (val) {
      this.changeSizeFlag = true
      this.pageNum = 1
      this.pageSize = val
      this.getList()
    },
    handleCurrentChange (val) {
      if (this.changeSizeFlag === false) {
        this.pageNum = val
        this.getList()
      }
    },
    // 排序
    sortChange: function (column, prop, order) {
      this.Sortkey = column.prop
      if (column.order === 'ascending') {
        this.Isace = 'asc'
      } else {
        this.Isace = 'desc'
      }
      this.getList()
    },
    getList (id) {
      this.listLoading = true
      this.$http
        .get(this.$http.defaults.meetingAPI + `api/meetingBusTask/getById?id=` + this.$route.params.id)
        .then(response => {
          this.project = response.data.result.name
        })
        .catch(error => {
          this.$message({
            message: '网络获取失败:' + error,
            type: 'error'
          })
        })
      this.$http
        .post(this.$http.defaults.meetingAPI + `api/meetingBusItem/queryList`, {taskId: this.$route.params.id})
        .then(response => {
          this.list = response.data.result
          if (id) {
            this.toggleSelection(id)
          }
          this.listLoading = false
        })
        .catch(error => {
          this.listLoading = false
          this.$message({
            message: '网络获取失败:' + error,
            type: 'error'
          })
        })
    },
    tableresize (obj) {
      var clientHeight = 0
      if (document.body.clientHeight && document.documentElement.clientHeight) {
        clientHeight = (document.body.clientHeight < document.documentElement.clientHeight) ? document.body.clientHeight : document.documentElement.clientHeight
      } else {
        clientHeight = (document.body.clientHeight > document.documentElement.clientHeight) ? document.body.clientHeight : document.documentElement.clientHeight
      }
      let myheight = 255

      obj.$nextTick(function () {
        obj.tableHeight = clientHeight - myheight
      })
    }
  },
  mounted () {
    this.getList()
    this.tableresize(this)
    window.onresize = () => {
      this.tableresize(this)
    }
  }
}
</script>
<style>
#cluereport .el-drawer__wrapper{
  width: 100%;
  height: 100%;
  position: fixed !important;
  overflow: hidden;
}
  .unitbox .el-table th{
    font-weight: normal;
    background-image: linear-gradient(to bottom , #f8f8f8, #efefef);
  }
  .unitbox .el-table td, .el-table th{
    padding:0;
    font-size:var(--size14) !important;
    line-height: 20px;
    font-family: '宋体'
  }
  .tableth{
    background-image: linear-gradient(to bottom , #f8f8f8, #efefef);
    height:33px;
  }
  #testlist .wboxs{
    height:45px;
    padding-top:10px;
  }
  #cluereport .el-table td{
    border-right: 1px solid #95B8E7;
    border-bottom: 1px solid #95B8E7;
  }
</style>
<style scoped>
  #cluereport .el-button{
    color:#fff !important;
  }
  #cluereport h1{
    font-size:var(--size16) !important;
    font-weight: bolder;
    height: 30px;
    border-bottom: 1px solid #95B8E7;
    font-family: '宋体';
    line-height: 30px;
    color: #000;
    padding-left:10px;
    background-image: linear-gradient(to bottom , #EEF4FF, #E0ECFF);
    margin:0;
  }
  .el-pagination{
    float: left;
    padding:7px 10px;
  }
  #cluereport .wboxs{
    width: 100%;
    background: #fff;
    border: 1px solid #95B8E7;
    margin:0 10px;
    overflow: hidden;
    padding:0;
  }
  .unitbox{
    padding:0;
    width: calc(100% - 0px);
    background: #fff;
    border: 1px solid #95B8E7;
    margin:0 10px;
    overflow: hidden;
  }
</style>
